[Part 2] Websphere Liberty Profile: OSGi, JPA, JAX-RS, and BPM
Part 2 of 4 Part Series:
ReST Services with JAX-RS
Part 2: ReST Services with JAX-RS (this article)
Part 4: Integrated BPM and ReST using OSGi bundles [work-in-progress]
Development Platform and Tools:
(see "resources" for additional equipment)
- Windows 7-64
- Eclipse Indigo SR2 (see requirements on Liberty tools download page)
- WebSphere® Application Server tools, V8.5 Liberty Profile
- Liberty server v8.5.0.2: wlp-developers-8.5.0.2.jar
- Database: Derby 10.10.1.1 (using Networked configuration)
NOTE: Though the new, extended beta (refresh) had many nice features... (i.e. CXF?) I ran into a few issues that required that I drop back to the GA release. I'll demonstrate the new beta-refresh in another write-up.
Reference:
Apache ARIES Blueprint tutorial
Blueprint Services by Guillaume Nodet (slideshare)
OSGi JAX-RS and bnd declerative service
Use Apache Wink with the Jackson JSON processor
Resources:
Download: WebSphere® Application Server Developer Tools
Download: WebSphere Application Server Liberty Profile
BairTail (win tail utility)
OSGi and JAX-RS (wink implementation) on Liberty Profile
NOTE: You'll see me starting with a project named "osgi_jaxrs_03a"... I already created the JAX-RS project but then wanted to try a few things before finally documenting the details.
1) Create the JAX-RS OSGi Bundle
Create the Project
file -> new -> other -> "osgi" filter -> OSGi Bundle Project
-> next
Give the project a name
Custom: checked
-> click "Advanced"
Check the following:
- Dynamic Web Module
- Java
- JAX-RS
- OSGi Bundle
-> OK
Leave other options unchecked
- form should resemble following
-> Next
Java
Leave defaults
-> Next
Web Module
NOTE: though we leave "generate web.xml" unchecked, the JAX-RS facet should later generate one for the project (if you have issues with the JAX-RS/Wink servlet, go back and generate the "web.xml")
Generate web.xml... : Leave this unchecked
-> next
OSGi Bundle
Version: 1.0.0
Generate an activator: unchecked
-> Next
JAX-RS Capabilities
JAX-RS Implementation Library
Type: Disable Library Configuration
Update Deployment Descriptor: checked
Liberty provides the JAX-RS libraries (Wink) via its runtime configuration. We do not deploy additional JAX-RS runtime libraries.
-> Finish
OSGi JAX-RS project created
2) Configure Manifest
Manifest dependencies should resemble the following:
Source view:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: osgi_jaxrs_03a
Bundle-SymbolicName: osgi_jaxrs_03a
Bundle-Version: 1.0.0
Bundle-ClassPath: WEB-INF/classes
Bundle-RequiredExecutionEnvironment : JavaSE-1.6
Web-ContextPath: /osgi_jaxrs_03a
Import-Package: com.ibm.websphere.jaxrs.server;version="1.0.0",
javax.el;version="2.0",
javax.servlet;version="2.5",
javax.servlet.annotation,
javax.servlet.http;version="2.5",
javax.servlet.jsp;version="2.0",
javax.servlet.jsp.el;version="2.0",
javax.servlet.jsp.tagext;version="2.0",
javax.ws.rs;version="1.0.0",
javax.ws.rs.core;version="1.0.0",
javax.ws.rs.ext;version="1.0.0",
javax.xml.bind.annotation
3) Create a simple JAX-RS test service
NOTE: This one, due to the fact I'm using a List, cannot be translated to XML. It works well enough though in producing JSON. We'll later create something that produces both XML and JSON.
-----
package colorservice;
import java.util.Arrays;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path(value= "/servicetest")
public class ServiceTest {
@GET
@Produces(value="application/json")
public List<String> GetList() {
return Arrays. asList(new String [] { "Hello Earth!", "Hello Mars!" });
}
public ServiceTest() {
System. out.println( "*** ServiceTest()");
}
}
-----
Service should be visible in project explorer
4) Create a class that extends Application
This class simply tells the framework to scan source for services
NOTE: The new beta Liberty JAX-RS library may not require implementation of "getClasses()" - However, this method does work on both v8502 and v85 beta refresh.
-------
package colorservice;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class AppService extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
// URL http://babe:9080/osgi_jaxrs_03a/jaxrs/servicetest
classes.add(ServiceTest. class);
return classes;
}
}
-------
4a) Add an Initialization Parameter for javax.ws.rs.Application
web.xml source view:
<?xml version="1.0" encoding= "UTF-8"?>
<web-app xmlns= "http://java.sun.com/xml/ns/javaee" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version= "3.0">
<display-name >osgi_jaxrs_03a </display-name >
<servlet >
<description >
JAX-RS Tools Generated - Do not modify </description >
<servlet-name >JAX-RS Servlet</ servlet-name>
<servlet-class >com.ibm.websphere.jaxrs.server.IBMRestServlet </servlet-class >
<init-param >
<param-name >javax.ws.rs.Application </param-name >
<param-value >colorservice.AppService </param-value >
</init-param >
<load-on-startup >1 </load-on-startup >
<enabled >true </enabled >
<async-supported >false </async-supported >
</servlet >
<servlet-mapping >
<servlet-name >JAX-RS Servlet</ servlet-name>
<url-pattern >
/ jaxrs/*</url-pattern >
</servlet-mapping >
</web-app>
5) Create OSGi Application project and Deploy/Test
OSGi Application Project
Add the osgi_jaxrs bundle
osgi_jaxrs_03a
-> Finish
6) Deploy and Test
Review Liberty server configuration
This is my Liberty featureManager configuration
- make sure you have jaxrs available (Liberty will help guide server configuration)
<featureManager >
<feature >jsp-2.2</ feature>
<feature >json-1.0</ feature>
<feature >blueprint-1.0 </feature >
<feature >beanvalidation-1.0</ feature>
<feature >jaxrs-1.1</ feature>
<feature >jndi-1.0</ feature>
<feature >servlet-3.0</ feature>
<feature >jdbc-4.0</ feature>
<feature >localConnector-1.0 </feature >
<feature >osgi.jpa-1.0 </feature >
<feature >jpa-2.0</ feature>
<feature >wab-1.0</ feature>
</featureManager >
Right-click service to verify/get URI value
Test in browser
== DONE =============